<?php
require_once("dao/IDao.php");

class ResultDAO implements IDao{
	var $conn;
	
	public function __construct($conn){
		$this->conn =& $conn;
	}
	
	public function getResultByYear($eventid){
		$stats = array();
		try {
			if($stmt = $this->conn->prepare("CALL GET_STANDING_BY_EVENT_ID(?)")){
				$stmt->bind_param("i",$eventid);
				$stmt->execute();
				
				$stmt->bind_result($_playerid,
						$_player,
						$_castlecreekOut,$_castlecreekIn,$_castlecreekTotal,$_castlecreekPar,$_castlecreekHoleIn1,
						$_stonebridgeOut,$_stonebridgeIn,$_stonebridgeTotal,$_stonebridgePar,$_stonebridgeHoleIn1,
						$_millpondOut,$_millpondIn,$_millpondTotal,$_millpondPar,$_millpondHoleIn1,
						$_hagosOut,$_hagosIn,$_hagosTotal,$_hagosPar,$_hagosHoleIn1,
						$_grandtotal,$_grandpar,$_grandholeIn1);
				
				while($stmt->fetch()){
					$stat = array();
					$stat["playerid"] = $_playerid;
					$stat["player"] = $_player;
					$stat["castlecreek_out"] = $_castlecreekOut;
					$stat["castlecreek_in"] = $_castlecreekIn;
					$stat["castlecreek_total"] = $_castlecreekTotal;
					$stat["castlecreek_par"] = $_castlecreekPar;
					$stat["castlecreek_holein1"] = $_castlecreekHoleIn1;
					
					$stat["stonebridge_out"] = $_stonebridgeOut;
					$stat["stonebridge_in"] = $_stonebridgeIn;
					$stat["stonebridge_total"] = $_stonebridgeTotal;
					$stat["stonebridge_par"] = $_stonebridgePar;
					$stat["stonebridge_holein1"] = $_stonebridgeHoleIn1;
					
					$stat["millpond_out"] = $_millpondOut;
					$stat["millpond_in"] = $_millpondIn;
					$stat["millpond_total"] = $_millpondTotal;
					$stat["millpond_par"] = $_millpondPar;
					$stat["millpond_holein1"] = $_millpondHoleIn1;
					
					$stat["hagos_out"] = $_hagosOut;
					$stat["hagos_in"] = $_hagosIn;
					$stat["hagos_total"] = $_hagosTotal;
					$stat["hagos_par"] = $_hagosPar;
					$stat["hagos_holein1"] = $_hagosHoleIn1;
					
					$stat["grand_total"] = $_grandtotal;
					$stat["grand_par"] = $_grandpar;
					$stat["grand_holein1"] = $_grandholeIn1;
					
					array_push($stats,$stat);
				}
				
			}else{
				throw new Exception("Statement preparation failed in class:" . __CLASS__);
			}
		} catch (Exception $e) {
			throw new Exception("Statement preparation failed in class:" . __CLASS__."\t".$e->getMessage());
		}
		$this->conn->close();
		return $stats;
	}
	
	public function getChampions(){
		$championList = array();
		try {
			if($stmt = $this->conn->prepare("CALL GET_CHAMPIONS()")){
				$stmt->execute();
				
				$stmt->bind_result($_eventid,$_year,$_playerid,$_firstname,$_lastname);
				while($stmt->fetch()){
					$stat = array();
					$stat["eventid"] = $_eventid;
					$stat["year"] = $_year;
					$stat["playerid"] = $_playerid;
					$stat["firstname"] = $_firstname;
					$stat["lastname"] = $_lastname;
					
					array_push($championList,$stat);
				}
				
			}
			else{
				throw  new Exception("Statement preparation failed in class:" . __CLASS__);
			}
			
		} catch (Exception $e) {
			throw new Exception("Statement preparation failed in class:" . __CLASS__."\t".$e->getMessage());
		}
		
		return $championList;
	}
	
	public function getSureshotWinners(){
		$sureshotwinners = array();
		try {
			if($stmt = $this->conn->prepare("CALL GET_SURESHOT_WINNERS()")){
				$stmt->execute();
		
				$stmt->bind_result($_eventid,$_year,$_playerid,$_firstname,$_lastname,$_grand_holein1);
				while($stmt->fetch()){
					$stat = array();
					$stat["eventid"] = $_eventid;
					$stat["year"] = $_year;
					$stat["playerid"] = $_playerid;
					$stat["firstname"] = $_firstname;
					$stat["lastname"] = $_lastname;
					$stat["grand_holein1"] = $_lastname;
					array_push($sureshotwinners,$stat);
				}
		
			}
			else{
				throw  new Exception("Statement preparation failed in class:" . __CLASS__);
			}
				
		} catch (Exception $e) {
			throw new Exception("Statement preparation failed in class:" . __CLASS__."\t".$e->getMessage());
		}
		
		return $sureshotwinners;
	}
	
	public function getItem($eventid){}
	public function saveItem($eventid){}
	public function deleteItem($eventid){}
	public function __destruct(){}
}

?>